package com.liujit.minio.service;

import com.liujit.minio.domain.FileUploadResult;
import com.liujit.minio.exception.FileUploadException;
import com.liujit.minio.properties.MinioProperties;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import java.io.InputStream;
import java.util.Calendar;

/**
 * @author: liujun
 * @date: 2021/6/19 12:59 上午
 * @description: 存储桶操作Service
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({ MinioProperties.class })
public class FileService implements InitializingBean {

    private BucketService bucketService;

    private MinioProperties minioProperties;

    private MinioClient client;

    public FileService(BucketService bucketService, MinioProperties minioProperties) {
        this.bucketService = bucketService;
        this.minioProperties = minioProperties;
    }

    @Override
    public void afterPropertiesSet() {
        this.client = new MinioClient.Builder()
                .endpoint(minioProperties.getEndpoint(), Integer.valueOf(minioProperties.getPort()), false)
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey()).build();
    }

    /**
     * 获取bucket名称
     */
    public String getBucketName() {
        return minioProperties.getBucketName();
    }

    /**
     * 以流的形式下载一个对象
     *
     * @param objectName
     * @return
     * @author: liujun
     * @date: 2021/6/19 1:10 上午
     */
    @SneakyThrows
    public InputStream getObject(String objectName) {
        String bucketName = minioProperties.getBucketName();
        return this.getObject(bucketName, objectName);
    }

    /**
     * @param bucketName
     * @param objectName
     * @return
     * @author: liujun
     * @date: 2021/6/19 1:10 上午
     */
    @SneakyThrows
    public InputStream getObject(String bucketName, String objectName) {
        GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(bucketName).object(objectName).build();
        return client.getObject(getObjectArgs);
    }

    /**
     * 通过InputStream上传对象
     *
     * @param inputStream
     * @return
     * @author: liujun
     * @date: 2021/6/19 1:07 上午
     */
    public FileUploadResult putObject(InputStream inputStream) {
        String bucketName = minioProperties.getBucketName();
        return this.putObject(bucketName, inputStream);
    }

    /**
     * 通过InputStream上传对象
     *
     * @param bucketName
     * @param inputStream
     * @return
     * @author: liujun
     * @date: 2021/6/19 1:07 上午
     */
    public FileUploadResult putObject(String bucketName, InputStream inputStream) {
        FileUploadResult result = new FileUploadResult();
        bucketService.makeBucket(bucketName);
        // 获取文件夹路径-当前日期
        Calendar calendar = Calendar.getInstance();
        // 转换路径
        String path = "/" + calendar.get(Calendar.YEAR) + "/" + (calendar.get(Calendar.MONTH) + 1) + "/" + calendar
                .get(Calendar.DATE) + "/" + calendar.getTimeInMillis();
        try {
            PutObjectArgs putObjectArgs = PutObjectArgs.builder().bucket(bucketName).object(path)
                    .contentType(MediaType.ALL_VALUE).stream(inputStream, inputStream.available(), -1).build();
            client.putObject(putObjectArgs);
        } catch (Exception e) {
            throw new FileUploadException();
        }
        result.setPath("/" + bucketName + path);
        return result;
    }

    /**
     * 删除一个对象
     *
     * @param objectName
     * @return
     * @author: liujun
     * @date: 2021/6/19 1:07 上午
     */
    @SneakyThrows
    public void removeObject(String objectName) {
        String bucketName = minioProperties.getBucketName();
        this.removeObject(bucketName, objectName);
    }

    /**
     * 删除一个对象
     *
     * @param bucketName
     * @param objectName
     * @return
     * @author: liujun
     * @date: 2021/6/19 1:07 上午
     */
    @SneakyThrows
    public void removeObject(String bucketName, String objectName) {
        RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build();
        client.removeObject(removeObjectArgs);
    }
}
